<?php

namespace App\Http\Controllers;


class DataController extends Controller
{
	function base(){
    if(empty($_GET["date"]) || strtotime($_GET["date"]) == ''){
      return "date error";
    }
    $date = $_GET["date"];
    $userHash = [];
    $userIds  = [];
    $userCount = ["nj"=>[],"xb"=>[]];

    $dbHost = env("DB_HOST");
    $dbUser = env("DB_USERNAME");
    $dbPassword = env("DB_PASSWORD");
    $dbName = "ptime3user";
    $dsn    = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
  	try {
  	    $dbh = new \PDO($dsn, $dbUser, $dbPassword,["cah"]); //初始化一个PDO对象

  	    $userResult = $dbh->query("SELECT id,name,description,phone,school_id,icon,sex,is_verified,created_at FROM ur_user WHERE created_at LIKE '$date %';");

  	    foreach($userResult as $row) {
  	        $userHash[$row["id"]] = $row;
            if($row["is_verified"])
              $userIds[] = $row["id"];
  	    }

        $totalVerifiedUserCountResult = $dbh->query("SELECT count(id) AS count FROM ur_user WHERE is_verified = 1;")->fetchColumn();

  	    $dbh = null;
  	} catch (\PDOException $e) {
  	    die ("Error!: " . $e->getMessage() . "<br/>");
  	}

    $dbName = "ptime3campus";
    $dsn    = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
  	try {
  	    $dbh = new \PDO($dsn, $dbUser, $dbPassword,["cah"]); //初始化一个PDO对象

  	    $userIntroResult = $dbh->query("SELECT user_id,intro FROM cp_user_system
          WHERE user_id In (".implode($userIds,",").") AND intro is not null");
        if(!empty($userIntroResult))
    	    foreach($userIntroResult as $row) {
    	        $userHash[$row["user_id"]]["intro"] = json_decode($row["intro"],true)["data"];
              @$userCount["nj"][substr($userHash[$row["user_id"]]["school_id"],0,3)]++;
              @$userCount["xb"][$userHash[$row["user_id"]]["sex"]]++;
    	    }
  	    $dbh = null;
  	} catch (\PDOException $e) {
  	    die ("Error!: " . $e->getMessage() . "<br/>");
  	}

    //return $userCount;
    //return $userHash;
    return view('data/base',[
      "users"=>$userHash,
      "userCount"=>$userCount,
      "verifiedUserCount"=>count($userIds),
      "totalVerifiedUserCount"=>$totalVerifiedUserCountResult
    ]);

  }
}
